package view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.example.cd5160866.materialdesgin.R;

/**
 * Created by cd5160866 on 2017/3/7.
 */

public class InputTextView extends View {
    private int mainColor = Color.parseColor("#2F333B");

    private int focuseColor = Color.parseColor("#EA413A");

    private int disAbleColor = Color.parseColor("#BCC1CC");

    private float textSize = 54;

    private Paint mPaint = new Paint();

    private boolean foucuseAble = false;

    private boolean hideInput = false;

    private boolean qrModel = false;

    private boolean isEnable = true;

    public InputTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.InputTextView);
        mainColor = a.getColor(R.styleable.InputTextView_mainColor,
                Color.parseColor("#2F333B"));
        focuseColor = a.getColor(R.styleable.InputTextView_focuseColor,
                Color.parseColor("#EA413A"));
        textSize = a.getDimension(
                R.styleable.InputTextView_textSize, 18);
        mText = a.getString(R.styleable.InputTextView_inputText);
        foucuseAble = a.getBoolean(R.styleable.InputTextView_isFocuse, false);
        hideInput = a.getBoolean(R.styleable.InputTextView_hideInput, false);
        qrModel = a.getBoolean(R.styleable.InputTextView_qrModel, false);
        isEnable = a.getBoolean(R.styleable.InputTextView_isEnable, true);
        if (TextUtils.isEmpty(mText)) {
            mText = "";
            isEnable = false;
        }
        a.recycle();
        init();
    }

    public InputTextView(Context context) {
        super(context);
        init();
    }

    private void init() {
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(dip2pixel(1));
        mPaint.setColor(mainColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawColor(Color.GREEN);
        if (qrModel)
            drawQrCodeEditText(canvas);
        else
            drawEditText(canvas);

    }

    private void drawEditText(Canvas canvas) {
        if (!isEnable) {
            mPaint.setColor(disAbleColor);
        } else {
            if (isFoucuseAble())
                mPaint.setColor(focuseColor);
            else
                mPaint.setColor(mainColor);
        }
        drawLine(canvas);
        if (hideInput)
            drawPassWordText(canvas);
        else
            drawText(canvas);

    }

    private void drawQrCodeEditText(Canvas canvas) {
        drawQrCodeLine(canvas);
        drawQrText(canvas);
    }

    private int getQrLineWidth() {
        return (getWidth() - getPaddingLeft() - getPaddingRight()) / 5;
    }

    private int getDrawStartX() {
        return 0 + getPaddingLeft();
    }

    /**
     * 画验证码线
     *
     * @param canvas
     */
    private void drawQrCodeLine(Canvas canvas) {
        float liney = getHeight() - mPaint.getStrokeWidth() / 2;
        float lineWidht = getQrLineWidth();
        int linex = getDrawStartX();
        for (int i = 0, size = 4; i < size; i++) {
            if (isFoucuseAble())
                mPaint.setColor(focuseColor);
            else
                mPaint.setColor(i > mText.length() - 1 ? disAbleColor : mainColor);
            canvas.drawLine(linex, liney, linex + lineWidht, liney, mPaint);
            linex = (int) (linex + lineWidht + lineWidht / 3);
        }


    }

    /**
     * 画验证码
     *
     * @param canvas
     */
    private void drawQrText(Canvas canvas) {
        if (TextUtils.isEmpty(mText))
            return;
        mPaint.setColor(isFoucuseAble() ? focuseColor : mainColor);
        float lineWidht = getQrLineWidth();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(textSize);
        mPaint.getTextBounds(mText, 0, 1, rect);
        float textY = getHeight() - mPaint.getStrokeWidth() / 2 - rect.height() / 2;
        float textX = getPaddingLeft() + lineWidht / 2 - rect.width() / 2;
        for (int i = 0, size = mText.length(); i < size; i++) {
            canvas.drawText((mText.substring(i, i + 1)), textX, textY, mPaint);
            textX = textX + lineWidht + lineWidht / 3;
        }
    }

    private int getDrawAreaWidth() {
        return getWidth() - getPaddingLeft() - getPaddingRight();
    }

    /**
     * 画底部输入线
     *
     * @param canvas
     */
    private void drawLine(Canvas canvas) {
        canvas.drawLine(getDrawStartX(), getHeight() - mPaint.getStrokeWidth() / 2, getDrawAreaWidth() + getDrawStartX(), getHeight() - mPaint.getStrokeWidth() / 2, mPaint);
    }

    private Rect rect = new Rect();

    //    private String mText = "This is Test 我的妈啊 仅仅是个测试。";
    private String mText = "8211";

    private void drawText(Canvas canvas) {
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(textSize);
        mPaint.getTextBounds(mText, 0, mText.length(), rect);
        Log.d("qw", "bound width" + rect.width() + " hight " + rect.height());
        canvas.drawText(mText, getDrawStartX(), getHeight() - mPaint.getStrokeWidth() / 2 - rect.height() / 2, mPaint);
    }

    /**
     * 画密码原点
     *
     * @param canvas
     */
    private void drawPassWordText(Canvas canvas) {
        int radius = dip2pixel(6);
        float ovalx = radius + getDrawStartX();
        float ovaly = (getHeight() - mPaint.getStrokeWidth() - radius * 2);
        for (int i = 0, size = mText.length(); i < size; i++) {
            canvas.drawCircle(ovalx, ovaly, radius, mPaint);
            ovalx = (ovalx + radius * 3);
        }
    }

    public void setIsEnable(boolean enable) {
        isEnable = enable;
        postInvalidate();
    }

    public void setDisAbleColor(int disAbleColor) {
        this.disAbleColor = disAbleColor;
        postInvalidate();
    }

    public void setText(String mText) {
        this.mText = mText;
        postInvalidate();
    }

    public void setMainColor(int mainColor) {
        this.mainColor = mainColor;
        postInvalidate();
    }


    public void setFocuseColor(int focuseColor) {
        this.focuseColor = focuseColor;
        postInvalidate();
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
        postInvalidate();
    }

    public void setFoucuseAble(boolean foucuseAble) {
        this.foucuseAble = foucuseAble;
        postInvalidate();
    }

    public void setHideInput(boolean hideInput) {
        this.hideInput = hideInput;
        postInvalidate();
    }

    public void setQrModel(boolean qrModel) {
        this.qrModel = qrModel;
        postInvalidate();
    }

    public int getDisAbleColor() {
        return disAbleColor;
    }

    public int getMainColor() {
        return mainColor;
    }

    public int getFocuseColor() {
        return focuseColor;
    }

    public float getTextSize() {
        return textSize;
    }

    public boolean isFoucuseAble() {
        return foucuseAble;
    }

    public boolean isHideInput() {
        return hideInput;
    }

    public boolean isQrModel() {
        return qrModel;
    }

    public String getmText() {
        return mText;
    }

    int defultWidth = 100;

    int defultheight = 50;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int finalWidth = 0;
        int finaLHeight = 0;
        if (widthMode == MeasureSpec.EXACTLY) {
            finalWidth = widthSize;
        } else {
            finalWidth = (int) (getPaddingLeft() + defultWidth + getPaddingRight());
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            finaLHeight = heightSize;
        } else {
            finaLHeight = (int) (getPaddingTop() + defultheight + getPaddingBottom());
        }
        setMeasuredDimension(finalWidth, finaLHeight);
    }

    public int dip2pixel(float n) {
        int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, n, getContext().getResources().getDisplayMetrics());
        return value;
    }
}
